/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package meg.web;

import java.io.Serializable;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import meg.dto.PlayerException;
import meg.ejb.entity.Player;
import meg.ejb.inter.PlayerUtil;
import meg.web.util.FacesMessageUtil;
import meg.web.util.MethodResult;

/**
 *
 * @author radek
 */
@ManagedBean
@SessionScoped
public class PlayerInfo implements Serializable {
    
    @EJB
    private meg.ejb.inter.PlayerInfo playerInfo;

    @EJB
    private PlayerUtil playerUtil;

    @Size(min=1, message="Login nie może być pusty")
    private String login;

    @Size(min=1, message="Hasło nie może być puste")
    private String password;

    @Size(min=5, max=15, message="Login musi być długości od 5 do 15 znaków")
    private String login2;

    @Pattern(regexp = "^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[a-zA-Z]{2,4}$" , message="Nieprawidłowy adres email")
    private String email;

    @Size(min=8, message="Hasło musi być długości co najmniej 8 znaków")
    private String password2;

    private String password3;

    public Player getLoggedPlayer() {
        return playerInfo.getLoggedPlayer();
    }

    public boolean isPlayerLogged() {
        return playerInfo.isPlayerLogged();
    }

    public String loginAction() {
        try {
            playerInfo.login(login, password);
            resetBeanFields();
            return MethodResult.SUCCESS;
        } catch (PlayerException ex) {
            FacesMessageUtil.error("Nie istnieje użytkownik o podanym loginie i haśle");
            return null;
        }
    }

    public String logoutAction() {
        playerInfo.logout();
        String goodbyeLogin = this.login;
        FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
        FacesMessageUtil.info("Pomyślnie wylogowano. Trzymaj się "+goodbyeLogin+" :)");
        return MethodResult.SUCCESS;
    }

    public String registerAndLoginAction() {
        try {
            if(!password2.equals(password3)) {
                FacesMessageUtil.error("Podane hasło oraz powtórzone hasło muszą być takie same");
                return null;
            }
            playerUtil.registerPlayer(login2, password2);
            playerInfo.login(login2, password2);
            resetBeanFields();
            return MethodResult.SUCCESS;
        } catch (PlayerException ex) {
            FacesMessageUtil.error("Istnieje już użytkownik o podanym loginie");
            return null;
        }
    }

    private void resetBeanFields() {
        login2 = "";
        email = "";
        password = "";
        password2 = "";
        password3 = "";
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getLogin2() {
        return login2;
    }

    public void setLogin2(String login2) {
        this.login2 = login2;
    }

    public String getPassword2() {
        return password2;
    }

    public void setPassword2(String password2) {
        this.password2 = password2;
    }

    public String getPassword3() {
        return password3;
    }

    public void setPassword3(String password3) {
        this.password3 = password3;
    }
    
}
